package com.ruoyi.web.controller.contarct;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.enums.SystemStatusType;
import com.ruoyi.income.service.IHouseContarctIncomeService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contarct.domain.HouseContarct;
import com.ruoyi.contarct.service.IHouseContarctService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同Controller
 * 
 * @author hct
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/contarct/contarct")
public class HouseContarctController extends BaseController
{
    @Autowired
    private IHouseContarctService houseContarctService;

    @Autowired
    private IHouseContarctIncomeService houseContarctIncomeService;


    /**
     * 查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseContarct houseContarct)
    {
        startPage();
        if(houseContarct.getStatus() == null){
            houseContarct.setStatus(SystemStatusType.ENABLE.getCode());
        }
        List<HouseContarct> list = houseContarctService.selectHouseContarctList(houseContarct);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:export')")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseContarct houseContarct)
    {
        List<HouseContarct> list = houseContarctService.selectHouseContarctList(houseContarct);
        ExcelUtil<HouseContarct> util = new ExcelUtil<HouseContarct>(HouseContarct.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseContarctService.selectHouseContarctById(id));
    }

    /**
     * 新增合同
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseContarct houseContarct)
    {
        houseContarct.setCreateTime(new Date());
        houseContarct.setSignBy(getUserId());
        houseContarct.setCreateBy(getUsername());
        Object o = houseContarctService.insertHouseContarct(houseContarct);
        return AjaxResult.success("添加成功",o);
    }

    /**
     * 修改合同
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseContarct houseContarct)
    {
        String username = getUsername();
        Long userId = getUserId();
        houseContarct.setSignBy(userId);
        houseContarct.setUpdateBy(username);
        houseContarct.setUpdateTime(new Date());
        return toAjax(houseContarctService.updateHouseContarct(houseContarct,userId));
    }

    /**
     * 删除合同
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        try {
            int i = houseContarctService.udHouseContarctByIds(ids, getUsername());
            return AjaxResult.success("删除成功",i);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 合同审批接口
     *      1.修改合同状态
     *      2.按合同周期 生成账单
     *      3.每条账单生成固定的租金项 与 物业
     *      4.修改房间状态
     * @param id
     * @return
     */
    @RateLimiter(time = 1,count = 1)
    @PreAuthorize("@ss.hasPermi('contarct:contarct:confirm')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/confirm/{id}")
    public AjaxResult confirmContarct(@PathVariable("id") Long id){
        try {
            Object o = houseContarctService.contarctApply(id,getUserId(),getUsername());
            return AjaxResult.success("审核成功",o);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 合同审批接口
     *      1.修改合同状态
     *      2.按合同周期 生成账单
     *      3.每条账单生成固定的租金项 与 物业
     *      4.修改房间状态
     * @param id
     * @return
     */
    @RateLimiter(time = 1, count=1)
    @PreAuthorize("@ss.hasPermi('contarct:contarct:confirm')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/reject/{id}/{remake}")
    public AjaxResult rejectContarct(@PathVariable("id") Long id,@PathVariable("remake") String remake){
        try {
            Object o = houseContarctService.contarctReject(id,remake,getUserId(),getUsername());
            return AjaxResult.success("审核成功",o);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 合同详情关联账单数据
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:getAll')")
    @GetMapping(value = "/getAll/{id}")
    public AjaxResult getContarctAllData(@PathVariable("id") Long id){
        try {
            Object contarctData = houseContarctService.getContarctData(id);
            return AjaxResult.success("查询成功",contarctData);
        } catch (Exception e) {
            return AjaxResult.error("查询异常");
        }
    }


    @PreAuthorize("@ss.hasPermi('contarct:contarct:edit')")
    @PostMapping(value = "/maincome/{contractId}/{feeSetId}/{fee}")
    public AjaxResult makeAllInCome(@PathVariable("contractId") Long contractId,@PathVariable("feeSetId") Long feeSetId,@PathVariable("fee") Float fee){
        try {
            String username = getUsername();
            Object contarctData = houseContarctIncomeService.makeAllfeeItem(contractId, feeSetId, fee, username);
            return AjaxResult.success("费项插入成功",contarctData);
        } catch (Exception e) {
            return AjaxResult.error("费项插入异常");
        }
    }

    /**
     * 合同提交
     * @param contractId
     * @return
     */
    @RateLimiter(time = 1,count = 1)
    @PreAuthorize("@ss.hasPermi('contarct:contarct:edit')")
    @PostMapping(value = "/commit/{contractId}")
    public AjaxResult commitContract(@PathVariable("contractId") Long contractId){
        try {
            Object o = houseContarctService.commitContarct(contractId,getUsername(),getUserId());
            return AjaxResult.success("提交成功",o);
        } catch (Exception e) {
            return AjaxResult.error("提交失败");
        }
    }

    /**
     * 生成退租单的合同详情
     * @param contractId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:edit')")
    @PostMapping(value = "/settle/{contractId}")
    public AjaxResult contarctDetailForSettle(@PathVariable("contractId") Long contractId){
        try {
            Object houseContarc = houseContarctService.contarctDetailForSettle(contractId);
            return AjaxResult.success("生成退租单详情",houseContarc);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("退租申请失败");
        }
    }

    /**
     * 合同作废
     * @param contractId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contarct:contarct:edit')")
    @PostMapping(value = "/tovoid/{contractId}")
    public AjaxResult contarctToVoid(@PathVariable("contractId") Long contractId){
        try {
            houseContarctService.contarctToVoid(contractId,getUserId(),getUsername());
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询合同列表
     */
    @GetMapping("/daoqiList")
    public AjaxResult daoqiList()
    {
        List<HouseContarct> list = houseContarctService.getDaoqiHouseContarctList(45);
        return AjaxResult.success(list);
    }
}
